<?

session_start();
if($_SESSION["memberId"] == null || $_SESSION["memberId"] == "")
{
	header("Location:../login.php");
}else{
	$memberId = $_SESSION["memberId"];
}

//$memberId = 1;


if(isset($_GET['page']) && is_numeric($_GET['page'])){
	$page = $_GET['page'];
}else{
	$page = 1;
}
$perPage = 20;
$limit = "limit ".($page-1)*$perPage.",$perPage";

include_once("../Dao/db.php");


$err_msg = "";
$search_col_arr = array("id","name","url");
$selected = array("id"=>"","name"=>"","url"=>"");
$selected2 = array(1=>"",2=>"",3=>"",4=>"");

if($_GET['del']==1 && is_numeric($_GET['tagId'])){

	$sql = "DELETE FROM cvpoint WHERE id=".$_GET['tagId']." AND memberId=$memberId";
	mysql_query($sql);

}else if($_POST['search_flag']==1 && isset($_POST['search_col'])
 && isset($_POST['search_text']) && $_POST['search_text']!='' 
 && is_numeric($_POST['search_type']) && in_array($_POST['search_col'],$search_col_arr)){
 
 	$_POST = array_map("ch_sql",$_POST);
	
	$selected_n = $_POST['search_col'];
	$selected[$selected_n] = "selected";
	$selected_n2 = $_POST['search_type'];
	$selected2[$selected_n2] = "selected";
	
	switch($_POST['search_type']){
		case 1:
		//と一致する
			$where = $_POST['search_col']."='".$_POST['search_text']."'";
			$selected[1] = "checked";
		break;
		case 2:
		//をすべて含む
			$where = $_POST['search_col']." LIKE '%".$_POST['search_text']."%'";
		break;
		case 3:
		//からはじまる
			$where = $_POST['search_col']." LIKE '".$_POST['search_text']."%'";
		break;
		case 4:
			//全角スペースを半角スペースに変換
			$keyword = mb_ereg_replace("　", " ", $_POST['search_text']);		
			//スペースをまとめる
			$keyword = preg_replace('!\s+!', " ", $_POST['search_text']);	
			$tmp2 = explode(" ", $keyword);	
			
			$tmp3 = array();
			for($i=0;$i<count($tmp2);$i++){		
				array_push($tmp3,$_POST['search_col']." LIKE '%".$tmp2[$i]."%'");
			}
			
			$where = implode(" OR ",$tmp3);
		break;
		case 5:
			$where = $_POST['search_col']." NOT LIKE '%".$_POST['search_text']."%'";
		break;
	} 
 $sql = "SELECT * FROM cvpoint WHERE  memberId = $memberId AND $where";
 $result = mysql_query($sql);
 //echo $sql;
}

if(!isset($_POST['search_flag']) || $err_msg!=''){
	//成果地点一覧
	$sql = "select count(*) as cnt from cvpoint where memberId = $memberId";
	$result = mysql_query($sql);
	$row_cnt = mysql_fetch_array($result);
	$total = $row_cnt['cnt'];
	$sql = "select * from cvpoint where memberId = $memberId $limit";
	$result = mysql_query($sql);
}

//echo $sql;
function ch_sql($str){
	return preg_replace(array('/[~;\'\"]/','/--/'),'',$str);
}

function getNavigation($base,$page,$total,$perPage,$tag_query){
	$totalPage = ceil($total/$perPage);
	for($i=1;$i<=$totalPage;$i++){
			$str .= "<a  href='$base?page=$i$tag_query'>[$i]</a>";
	}

	return $str;
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<!-- DW6 -->
<head>
<!-- Copyright 2005 Macromedia, Inc. All rights reserved. -->
<title>タグ発行 > コンバージョンタグ</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" href="../css/common.css" type="text/css" />
<script language="JavaScript" src="../js/common.js" type="text/javascript"></script>
<script src="../SpryAssets/SpryValidationTextField.js" type="text/javascript"></script>
<link href="../SpryAssets/SpryValidationTextField.css" rel="stylesheet" type="text/css" />
</head>
<body bgcolor="#F4FFE4">
<?php include("../head.php") ?>
<table width="100%" border="0" cellspacing="0" cellpadding="0">

 <tr>
    <td width="151" valign="top" bgcolor="#5C743D"><?php include("../menu.php");?></td>
    <td width="813" colspan="2" align="center" valign="top" >
<!-- start of main area -->
<script type="text/javascript">
<!--

// 入力
function do_input(module, cv_point_id){
	var frm = document.mainform;
	ajax_do(frm, module, 'cv_point_id=' + cv_point_id, setFocus);
}
function setFocus(originalRequest){
	document.inputform.cv_point_name.focus();
}

// タグ表示
function do_refer(module, cv_point_id){
	var frm = document.mainform;
	ajax_do(frm, module, 'cv_point_id=' + cv_point_id);
}

// ダウンロード
function do_download(){
	var frm = document.mainform;
	sub_download(frm, 'tag_download');
}

// 入力値チェック
function input_check(){
	var frm = document.inputform;
	if(!checkRequire(frm.cv_point_name, '成果地点名')) return false;
	if(!checkRequire(frm.url, 'URL')) return false;

	return true;
}
// 登録処理
function do_save(module){
	var frm = document.inputform;
	if (input_check() == false) return false;
	if( !confirm_regist() ) return false;
	sub_do(frm, module);
}

-->
</script>
<?=$err_msg?>
<form action="TagList.php" method="post">
<table class="tbl where" border="0" cellpadding="4" cellspacing="0" width="100%">
	<tbody><tr>
		<td>
			<select name="search_col" class="txBox">
			  <option value="id" <?=$selected["id"]?>>ID </option>
			  <option value="name" <?=$selected["name"]?>>成果地点名 </option>
			  <option value="url" <?=$selected["url"]?>>URL
			</option>
			</select>
			<input name="search_text" size="35" maxlength="100" value="<?=$_POST['search_text']?>" class="txBox" type="text" >
<select name="search_type" class="txBox">
	<option value="2"  <?=$selected2[2]?>>を全て含む
	</option><option value="4" <?=$selected2[4]?>>のどれかを含む
	</option><option value="3" <?=$selected2[3]?>>で始まる
	</option><option value="1" <?=$selected2[1]?>>に等しい
	</option><option value="5" <?=$selected2[5]?>>を含まない
</option>
</select>
&nbsp;
<input name="search_flag" type="hidden" id="search_flag" value="1" />&nbsp;			<input class="btn search" value="" type="submit">
		</td>
	</tr>
</tbody></table>
<br />
</form>
<table border="0" cellpadding="2" cellspacing="0" width="100%">
	<tbody><tr>
		<td align="left">
		</td>
		<td align="right">
			<input class="btn new" onclick="location.href='TagRegist.php'" type="button">
			<input class="btn refresh" onclick="location.href='TagList.php'" type="button">		</td>
	</tr>
</tbody></table>

<table class="tbl" border="0" cellpadding="4" cellspacing="0" width="100%">
	<tbody><tr class="tr2" align="center">
		<td align="center" width="40">No</td>
		<td align="center">
			ID
		</td>
		<td width="20"></td>
		<td align="left">
			成果地点名
		</td>
		<td align="left">
			URL
		</td>
		<td align="center" width="60">
		    </td>
		<td></td>
	</tr>
<? 
$idx = 1;
if($result){
while($row = mysql_fetch_array($result)){
$tr_class = $idx%2==0 ? "tr4" : "tr3";
?>	
			<tr class="<?=$tr_class?>">				
			<td align="center"><?=$idx?></td>
			<td align="center"><?=$row['id']?></td>
			<td><input class="btn_ico btn_edit" title="編集" value="" onclick="location.href='TagEdit.php?memberId=<?=$memberId?>&tagId=<?=$row['id']?>'" type="button"></td>
			<td align="left"><?=$row['name']?></td>
			<td align="left"><?=$row['url']?></td>
			<td align="center">
				<input class="btn" value="タグ表示" onclick="window.open('TagDisp.php?tagId=<?=$row['id']?>', 'tagdisp', 'width=700, height=250, menubar=no, toolbar=no, scrollbars=yes');" type="button">
			</td>
			<td align="center"><input class="btn" value="削除" onclick="javascript:location.href='TagList.php?del=1&tagId=<?=$row['id']?>';" type="button"></td>
		</tr>
<? 
$idx++;
}
}?>		
</tbody></table>

<table border="0" cellpadding="2" cellspacing="0" width="100%">
	<tbody><tr>
		<td align="right"><label id="fromToPage" name="fromToPage"><? echo 1+($page-1)*$perPage;?>件-<? echo $total>$perPage*$page ? $perPage*$page : $total;?>件</label><label id="memberInfoCount" name="memberInfoCount">/<?=$total?>件</label></td>
		<td width="10"><? echo getNavigation("TagList.php",$page,$total,$perPage,$tag_query);?></td>	
	</tr>
</tbody></table><br>

</form><br>
	
<div id="input_area"></div>			<!-- end of main area -->   </td>

  </table>
<?php include("../foot.php");?>
</body>
</html>